<template>
    <el-container>
        <el-main class="wrapper access">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{'min-height': mainHeight+'px'}">
                <div class="panel-body">
                    <el-form  v-loading="loading" :model="ruleForm" ref="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
                        <el-input type="hidden" v-model="ruleForm.id"></el-input>
                        <el-checkbox label="" @change="checkAll" v-model="all" style="margin-left:33px">全部选择</el-checkbox>
                        <el-form-item v-for="(item,index) in authens" :label="item.name" :key="index">
                            <el-checkbox-group v-model="ruleForm.rules">
                                <el-checkbox v-for="(child,i) in item.child" :label="child.code" :key="i">{{child.name}}</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" :loading="buttonLoading" :disabled="formDisabled" @click="submitForm('ruleForm')">{{buttonText}}</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </section>
        </el-main>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {RoleAccess,RoleAccessUpdate} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "权限管理"},],
                all: false,
                buttonLoading : false,
                buttonText: '提 交',
                formDisabled : false,
                ruleForm: {
                    id: 0,
                    rules: [],
                },
                rules: {},
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/role');
            if (this.$refs.ruleForm!==undefined) {
                this.$refs.ruleForm.resetFields();
            }
            this.getAccess(this.$route.params.id);
            this.ruleForm.id = this.$route.params.id;
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading','authens'])
        },
        methods: {
            getAccess(rid){
                RoleAccess({params:{id:rid}}).then((response)=>{
                    if(response.code){
                        this.$message.error(response.msg);
                    }else{
                        this.ruleForm.rules = response.data.rules;
                    }
                })
            },
            checkAll(val){
                const value = [];
                this.authens.filter(auth=>{
                    auth.child.filter(a=>{
                        value.push(a.code);
                    })
                });
                this.ruleForm.rules = val ? value : [];
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(!this.ruleForm.rules.length){
                            this.$message.error('请至少选择一个角色拥有的权限');return false;
                        }else{
                            this.buttonLoading = true;
                            this.buttonText = '提交中...';
                            this.formDisabled = true;
                            RoleAccessUpdate(this.ruleForm).then((response)=>{
                                this.buttonLoading = false;
                                this.buttonText = '提 交';
                                this.formDisabled = false;
                                if(response.code){
                                    this.$message.error(response.msg);
                                }else{
                                    this.$message({
                                        type: 'success',
                                        message: response.msg,
                                        duration: '1000',
                                        onClose:() =>{
                                            this.$router.push({name:'Role'});
                                        }
                                    });
                                }
                            }).catch(error=>{
                                this.buttonLoading = false;
                                this.buttonText = '提 交';
                                this.formDisabled = false;
                                this.$message.error(error.response.statusText);
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>
<style>
    .access .el-form-item__content {
        line-height: 40px;
    }
</style>
